
package com.sskj.jkzx.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sskj.jkzx.domain.entity.OrgQueryParam;
import com.sskj.jkzx.domain.entity.org.OrgBaseInfo;
import com.sskj.jkzx.domain.entity.org.OrgCxInfo;
import com.sskj.jkzx.domain.entity.org.OrgEmpInfo;
import com.sskj.jkzx.domain.entity.org.OrgEngInfo;
import com.sskj.jkzx.domain.entity.org.OrgZzInfo;
import com.sskj.jkzx.domain.entity.quantity.OrgEmpPCTypeQuantity;
import com.sskj.jkzx.vo.TimeLineBean;
import com.sskj.utils.HttpHelp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "**/sskj/org")
@Api(value = "企业信息", description = "企业信息")
public class OrgInfoServiceImpl {

	protected final Log logger = LogFactory.getLog(OrgInfoServiceImpl.class);

	@Value("${sskj.cmisServer}")
	private String cmisServer;

	@Value("${sskj.jdServer}")
	private String jdServer;

	/***
	 * 企业基本信息
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/jbxx", method = RequestMethod.GET)
	@ApiOperation(value = "企业基本信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orgId", value = "企业ID", paramType = "form", dataType = "string") })
	public OrgBaseInfo queryOrgBaseInfo(@ApiIgnore OrgQueryParam orgQueryParam) {

		OrgBaseInfo result;
		try {
			String sgs = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/jkzx/org/jbxx/" + orgQueryParam.getOrgId(),
					null);

			result = JSONObject.parseObject(sgs, OrgBaseInfo.class);
		} catch (Exception e) {
			logger.info(e.getMessage());
			result = new OrgBaseInfo();
		}

		logger.info(JSONObject.toJSONString(result));

		return result;
	}

	/***
	 * 企业时间轴信息查询
	 * 
	 * @param orgQueryParam
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/timeline", method = RequestMethod.GET)
	@ApiOperation(value = "企业时间轴信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orgId", value = "企业ID", paramType = "form", dataType = "string") })
	public List<TimeLineBean> queryOrgTimelineInfo(@ApiIgnore OrgQueryParam orgQueryParam) {

		List<TimeLineBean> results = new ArrayList<TimeLineBean>();

		String str;
		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/org/orgxxlr/" + orgQueryParam.getOrgId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/org/orgxxba/" + orgQueryParam.getOrgId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/org/orgxxztb/" + orgQueryParam.getOrgId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/org/orgxxht/" + orgQueryParam.getOrgId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/org/orgxxsgxk/" + orgQueryParam.getOrgId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		sort(results, "time", true);

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 企业人员信息
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/ryxx", method = RequestMethod.GET)
	@ApiOperation(value = "企业人员信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orgId", value = "企业ID", paramType = "form", dataType = "string") })
	public List<OrgEmpInfo> queryOrgEmpInfo(@ApiIgnore OrgQueryParam orgQueryParam) {

		List<OrgEmpInfo> results = new ArrayList<OrgEmpInfo>();

		Map<String, Object> params = new HashMap<>();
		params.put("srvName", "ythpt");
		params.put("srvPwd", "ythpt");

		try {
			String ss = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/jkzx/org/ryxx/" + orgQueryParam.getOrgId(),
					params);

			if (ss != null) {
				results.addAll(JSON.parseArray(ss, OrgEmpInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 企业资质信息
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/zzxx", method = RequestMethod.GET)
	@ApiOperation(value = "企业资质信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orgId", value = "企业ID", paramType = "form", dataType = "string") })
	public List<OrgZzInfo> queryOrgZzInfo(@ApiIgnore OrgQueryParam orgQueryParam) {

		List<OrgZzInfo> results = new ArrayList<OrgZzInfo>();

		try {
			String ss = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/jkzx/org/zzxx/" + orgQueryParam.getOrgId(),
					null);

			if (ss != null) {
				results.addAll(JSON.parseArray(ss, OrgZzInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 企业诚信信息查询
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/cxfxx", method = RequestMethod.GET)
	@ApiOperation(value = "企业诚信信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orgId", value = "企业ID", paramType = "form", dataType = "string") })
	public List<OrgCxInfo> queryOrgCxInfo(@ApiIgnore OrgQueryParam orgQueryParam) {

		List<OrgCxInfo> results = new ArrayList<OrgCxInfo>();

		try {
			String ss = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/jkzx/org/cxfxx/" + orgQueryParam.getOrgId(),
					null);

			if (ss != null) {
				results.addAll(JSON.parseArray(ss, OrgCxInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 企业在建项目信息查询
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/zjxmxx", method = RequestMethod.GET)
	@ApiOperation(value = "企业在建项目信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orgId", value = "企业ID", paramType = "form", dataType = "string") })
	public List<OrgEngInfo> queryZOrgEngInfo(@ApiIgnore OrgQueryParam orgQueryParam) {

		List<OrgEngInfo> results = new ArrayList<OrgEngInfo>();

		try {
			String ss = HttpHelp.sendGet(jdServer + "/seam/resource/v1/jkzx/org/zjxmxx/" + orgQueryParam.getOrgId(),
					null);

			if (ss != null) {
				results.addAll(JSON.parseArray(ss, OrgEngInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 企业业绩信息查询
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/yjxmxx", method = RequestMethod.GET)
	@ApiOperation(value = "企业业绩信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orgId", value = "企业ID", paramType = "form", dataType = "string") })
	public List<OrgEngInfo> queryYOrgEngInfo(@ApiIgnore OrgQueryParam orgQueryParam) {

		List<OrgEngInfo> results = new ArrayList<OrgEngInfo>();

		try {
			String ss = HttpHelp.sendGet(jdServer + "/seam/resource/v1/jkzx/org/yjxmxx/" + orgQueryParam.getOrgId(),
					null);

			if (ss != null) {
				results.addAll(JSON.parseArray(ss, OrgEngInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/**
	 * 企业人员证书统计
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/pctj", method = RequestMethod.GET)
	@ApiOperation(value = "企业人员证书统计", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orgId", value = "企业ID", paramType = "form", dataType = "string") })
	public List<OrgEmpPCTypeQuantity> getOrgEmpPCTJ(@ApiIgnore OrgQueryParam orgQueryParam) {
		List<OrgEmpPCTypeQuantity> results = new ArrayList<OrgEmpPCTypeQuantity>();

		try {
			String ss = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/jkzx/org/pctj/" + orgQueryParam.getOrgId(),
					null);
			if (ss != null) {
				results.addAll(JSON.parseArray(ss, OrgEmpPCTypeQuantity.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/**
	 * @describe 依据某个字段对集合进行排序
	 * @author ...
	 * @date 2013-1-22 下午3:44:47
	 * @param list
	 *            待排序的集合
	 * @param fieldName
	 *            依据这个字段进行排序
	 * @param asc
	 *            如果为true，是正序；为false，为倒序
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public <T> void sort(List<T> list, String fieldName, boolean asc) {

		Comparator<?> mycmp = ComparableComparator.getInstance();
		mycmp = ComparatorUtils.nullLowComparator(mycmp); // 允许null
		if (!asc) {
			mycmp = ComparatorUtils.reversedComparator(mycmp); // 逆序
		}
		Collections.sort(list, new BeanComparator(fieldName, mycmp));
	}

}
